<?php
// @formatter:off
/**
 * @file c.collections.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-17 01:40:31 +0000 (Mon, 17 Jun 2013) $
 *
 * $Id: c.collections.php 75 2013-06-17 01:40:31Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/default/controlers/contents/c.collections.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_COLLECTION_LIST)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/pager.php";

	global $wcCollectionsHolder;
	global $wctDefaults;
	global $wcPageCache;

	if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["ACTION"])) {
		switch($_POST["ACTION"]) {
			case "REMOVE":
				if($wcProfile->allowedTo(WC_PERM_COLLECTION_EDIT)) {
					if(isset($_POST["ID"])) {
						$collection = $wcCollectionsHolder->item($_POST["ID"], $wcGroup);
						if($collection) {
							$collection->remove();
						}
					}
				}
				break;
		}
	}

	$wcPageCache->anyCollection();

	$page = isset($_REQUEST["page"]) ? $_REQUEST["page"] : 1;
	$minCollectionInPage = $wctDefaults["max-per-page"]["collections"] * ($page - 1);
	$maxCollectionInPage = $minCollectionInPage + $wctDefaults["max-per-page"]["collections"];

	$wcThemeAssigns["PATH"] = "Top Collections (page {$page})";
	$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.collections.html";

	//
	// Selecting only top collections in range.
	$collections = array();
	$items = $wcCollectionsHolder->topItems($wcGroup);
	$collectionCount = 0;
	uasort($items, "WCCollection::NameSort");
	foreach($items as $collection) {
		if($collection->ok()) {
			if($collectionCount >= $minCollectionInPage && $collectionCount < $maxCollectionInPage) {
				$collections[] = $collection;
			}
			$collectionCount++;
		}
	}

	wcSetPager(requestParams(true, $wcAction, array('page')), ceil($collectionCount / $wctDefaults["max-per-page"]["collections"]), $page);

	//
	// Creating multi-array by prefix
	$lists = array();
	$ids = 1;
	foreach($collections as $collection) {
		$name = $collection->name;
		$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
		if($prefix < 'A' || $prefix > 'Z') {
			$prefix = '#';
		}
		$prefix = "{$prefix}";
		if(!isset($lists[$prefix])) {
			$lists[$prefix] = array(
				"prefix" => $prefix,
				"list" => array()
			);
		}

		$lists[$prefix]["list"][] = wcGetCollectionPreviewAssigns($collection);
	}
	ksort($lists);

	$wcThemeAssigns["COLLECTIONS-LISTS"] = $lists;

	if($wcProfile->allowedTo(WC_PERM_COLLECTION_ADD)) {
		$wcThemeAssigns["COLLECTIONS-CREATE"] = true;
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to list collections";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>
